const app = getApp()
let Axios = require('../../common/js/ajax.js');
let WxValidate = require('../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
var page = 1;
var switch_p = true
Page({
	data: {
		openid: '',
		shopName: '',
		shopAvatarUrl: '',
		curentNav: 2, //当前导航
		totalPrice: 0,
		cur_id: 0,
		select: false,
		selectArr: [],
		selectPrice: 0,
		orders: [],
		store_id: 0
	},
	onLoad: function(e) {
		var _this = this;
		this.initValidate()
		this.load = this.selectComponent("#loading");
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		_this.setData({
			store_id: e.id
		})
		Axios.get(app.globalData.baseUrl + 'App/Capital/Cash/goods_payment_list', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			p: 1
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					orders: data.data,
					totalPrice: data.total_money
				})
				_this.load.loadIng()
			} else {
				_this.showModal(res)
			}
		})
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '货款提取'
		})
	},
	onReachBottom: function() {
		var _this = this;
		wx.showLoading({
			title: '玩命加载中',
		})
		if (switch_p) {
			switch_p = false
			page = page + 1;
			Axios.get(app.globalData.baseUrl + 'App/Capital/Cash/goods_payment_list?p=' + page, {
				uid: this.uid,
				time: this.timesp,
				sign: this.Md5,
				store_id: this.data.store_id,
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					var temp = _this.data.orders.concat(data.data)
					_this.setData({
						orders: temp,
					})
					switch_p = true
					wx.hideLoading();
				} else {
					_this.showModal(res)
				}
			})
		}
	},
	handleSelectTap: function(e) {
		var index = e.currentTarget.dataset.index;
		var id = this.data.orders[index].id;
		var select = this.data.orders[index].select;
		var temp = this.data.selectArr;
		var tempMoney = this.data.selectPrice;
		var curprize = this.data.orders[index].total_price
		curprize = Number(curprize)
		if (select == false) {
			temp.push(id);
			tempMoney += curprize
			this.setData({
				['orders[' + index + '].select']: true,
				selectArr: temp,
				selectPrice: tempMoney
			})
		} else {
			temp.splice(temp.indexOf(id), 1);
			tempMoney -= curprize
			this.setData({
				['orders[' + index + '].select']: false,
				selectArr: temp,
				selectPrice: tempMoney
			})
		}
		console.log(this.data.selectArr)
	},
	submit: function(e) {
		console.log(e.detail)
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		var _this = this
		var CONST = {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id
		}
		if(this.data.orders.length == 0){
			wx.showModal({
				content: '暂无可提货款订单',
				showCancel: false,
			})
			return false
		}
		if (e.order_id.length == 0) {
			e.order_id = 'all'
		}
		var obj = Object.assign(e, CONST);
		Axios.post(app.globalData.baseUrl + 'App/Capital/Cash/order_money',
			obj, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					wx.navigateTo({
						url: '/pages/drawing/drawSub/drawSub?id=' + this.data.store_id + '&ratio=' + data.ratio + '&bank_card=' +
							data.bank_card + '&bank_name=' + data.bank_name + '&money=' + data.money + '&service_money=' + data.service_money +
							'&order_id=' + e.order_id
					})
				} else {
					_this.showModal(res)
				}
			})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			order_id: {
				required: false,
			}
		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			order_id: {
				required: '请选择提取的订单',
			}
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	}
})
